<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class FileListDataProvider extends CMiniyunActiveDataProvider
{
    private $_list;
    
    private function _fillFileInfo(&$item, $fileItem)
    {
                                $userModel = User::model()->findByPk($fileItem['user_id']);
        if (!empty($userModel))
        {
            $item->author = $userModel['user_name'];
            $item->userID = $fileItem['user_id'];
        }
        if($fileItem['parent_file_id']!==0)
        {
                                                $parentFile = UserFile::model()->findByPk($fileItem['parent_file_id']);
            if (!empty($parentFile))
            {
                $item->directory = $parentFile['file_path'];
                $item->parentID  = $fileItem['parent_file_id'];
                                                                $item->retreatID = $parentFile['parent_file_id'];
            }
        }

        $item->fileName  = $fileItem['file_name'];
        $item->filePath  = $fileItem['file_path'];
        $item->versionID  = $fileItem['version_id'];

        if ($item->versionID == 0)
        {
            $item->fileSize = '文件夹';
        } else
        {
            if ($fileItem['file_size'] === 0)
            {
                $item->fileSize = '空文件';
            } else
            {
                $item->fileSize  = CUtils::getFormatSize($fileItem['file_size']);
            }
        }
        $item->isDeleted = $fileItem['is_deleted'];
        $item->updatedAt = CUtils::getFormatTime($fileItem['file_update_time']);
        $item->createdAt = CUtils::getFormatTime($fileItem['file_create_time']);
        if ($item->versionID)
        {
        	$mimeType =  FileVersion::model()->findBySql("select mime_type from ".Yii::app()->params['tablePrefix']."file_versions where id = ".$item->versionID);
            if(strpos($mimeType['mime_type'], 'image') !== false) {
        		$item->icon = "/index.php?r=thumbnail/index&size=small&tid=".$fileItem['id']."&ver=".$fileItem['version_id'];
        	} elseif (strpos($mimeType['mime_type'], 'msword') !== false){
        		$item->icon=Yii::app()->params["iconWord"];
        	} 
            elseif (strpos($mimeType['mime_type'], 'msexcel') !== false){
        		$item->icon=Yii::app()->params["iconExcel"];
        	}
            elseif (strpos($mimeType['mime_type'], 'mspowerpoint') !== false){
        		$item->icon=Yii::app()->params["iconPpt"];
        	}
            elseif (strpos($mimeType['mime_type'], 'text') !== false){
        		$item->icon=Yii::app()->params["iconText"];
        	}
            elseif (strpos($mimeType['mime_type'], 'video') !== false){
        		$item->icon=Yii::app()->params["iconVideo"];
        	}
            elseif (strpos($mimeType['mime_type'], 'audio') !== false){
        		$item->icon=Yii::app()->params["iconAudio"];
        	}
        	else {
            	$item->icon=Yii::app()->params["iconFile"];
        	}
        } else {
        	$item->icon=Yii::app()->params["iconFolder"];
        	
        }

        if (!isset($item->directory))
        {
                                                $item->directory = "根目录";
            $item->parentID  = 0;
        }
        if (!isset($item->author))
        {
            {
                                                                $item->author = "无名";
            }
        }
        if (!isset($item->retreatID))
        {
            $item->retreatID = 0;
        }

    }

    
    public function getDataById($id)
    {
        if($this->_list===null || $refresh){
                                    $this->dbModel = new UserFile;
            $this->getDbData("id=".$id, "file_type desc");
            $dbData = $this->dbData;
            $ids = $this->ids;

                        $data = array();
            foreach($dbData as $index=>$fileItem){
                $item = new UserBase();
                $key = $fileItem['id'];
                $item->id=$key;

                                $this->_fillFileInfo($item,$fileItem);

                array_push($data,$item);
            }
            $this->_list = $data;
        }
        return $this->_list;
    }

    
    public function getData($refresh=false){
        if($this->_list===null || $refresh){
                                    $this->model = new UserFile;
            $this->initData("");
            $ids = $this->ids;
                        $data = array();
            foreach($this->data as $index=>$fileItem){
                $item = new FileBase();
                $item->id = $fileItem['id'];
                $this->_fillFileInfo($item,$fileItem);                array_push($data,$item);
            }
            $this->_list = $data;
        }
        return $this->_list;
    }


}